<template>
    <div class="taobao-cart">
        <div v-for="(item) in shopCartData" :key="item.cartId" class="cart-item">
            <div class="check-box" @click="toggleCheck(item.cartId, item.checked)">
                <div class="check-circle" :class="{ checked: item.checked }">
                    <el-icon v-if="item.checked" class="check-icon">
                        <Check />
                    </el-icon>
                </div>
            </div>
            <div class="item-img">
                <img :src="item.mainImage" :alt="item.productName">
            </div>

            <div class="item-info">
                <h3 class="item-title">{{ item.productName }}</h3>
                <p class="price">￥{{ item.price }}</p>
            </div>

            <div class="quantity-control">
                <div class="quantity-buttons">
                    <button class="round-btn minus" @click="updateQuantity(item.cartId, '-')"
                        :disabled="item.quantity <= 1">-</button>
                    <span class="quantity">{{ item.quantity }}</span>
                    <button class="round-btn plus" @click="updateQuantity(item.cartId, '+')">+</button>
                </div>
                <div class="delete-btn" @click="deleteItem(item.cartId)">
                    <el-icon>
                        <Delete />
                    </el-icon>
                </div>
            </div>
        </div>
        <div v-if="!shopCartData || shopCartData.length === 0" class="empty-cart-message">
            购物车空空如也，快去购物吧！
        </div>
    </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue';
import { Check } from '@element-plus/icons-vue';
import { showToast } from 'vant';

const props = defineProps({
    shopCartData: {
        type: Array,
        default: () => []
    }
});
const emit = defineEmits(['changeShopCartData', 'changeNumber', 'deleteProduct']);

const toggleCheck = (cartId, currentCheckedState) => {
    emit('changeShopCartData', cartId, !currentCheckedState);
};

const updateQuantity = (cartId, sign) => {
    emit('changeNumber', cartId, sign);
};

const deleteItem = (cartId) => {
    emit('deleteProduct', cartId);
};
</script>

<style scoped>
/* 你的样式保持不变 */
.taobao-cart {
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    margin-bottom: 60px;
}

.cart-item {
    display: flex;
    padding: 15px;
    border-bottom: 1px solid #f5f5f5;
    align-items: center;
    position: relative;
}

/* 圆形选择按钮 */
.check-box {
    margin-right: 12px;
    cursor: pointer;
}

.check-circle {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 1px solid #ddd;
    display: flex;
    align-items: center;
    justify-content: center;
    /* transition: all 0.2s; */
}

.check-circle.checked {
    background-color: #ff5000;
    border-color: #ff5000;
}

.check-icon {
    color: white;
    font-size: 12px;
    font-weight: bold;
}

/* 左侧图片 */
.item-img {
    width: 80px;
    height: 80px;
    margin-right: 15px;
    flex-shrink: 0;
}

.item-img img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 4px;
}

/* 中间商品信息 */
.item-info {
    flex: 1;
    min-width: 0;
    padding-right: 15px;
}

.item-title {
    margin: 0 0 8px 0;
    font-size: 14px;
    color: #333;
    font-weight: normal;
    line-height: 1.5;
    display: -webkit-box;
    -webkit-line-clamp: 3; /* 已修改为三行 */
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
}

.price {
    color: #ff5000;
    font-size: 16px;
    font-weight: bold;
    margin: 0;
}

/* 右边圆形数量按钮 */
.quantity-control {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.quantity-buttons {
    display: flex;
    align-items: center;
    gap: 5px;
}

.delete-btn {
    position: absolute;
    top: 80px;
    right: 15px;
    bottom: 15px;
    color: #999;
    cursor: pointer;
    transition: all 0.2s;
    padding: 4px;
    border-radius: 4px;
}

.round-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid #ddd;
    background: #f5f5f5;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: #666;
    cursor: pointer;
    padding: 0;
    transition: all 0.2s;
}

.round-btn:hover {
    background: #eee;
}

.round-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.quantity {
    min-width: 30px;
    text-align: center;
    font-size: 14px;
}

/* 交互效果 */
.cart-item:hover {
    background: #fafafa;
}

.empty-cart-message {
    text-align: center;
    padding: 20px;
    color: #999;
    font-size: 16px;
}
</style>